package com.infore.siteManagement.OutSide.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.OutSide.Service.IStatisAndAlarmService;
import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.entity.Area;
import com.infore.siteManagement.entity.Basinn;
import com.infore.siteManagement.entity.Project;
import com.infore.siteManagement.entity.Station;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IAreaRespository;
import com.infore.siteManagement.repository.IBasinnRespository;
import com.infore.siteManagement.repository.IProjectAndStationRepository;
import com.infore.siteManagement.repository.IStationRepository;
import com.infore.siteManagement.repository.IndexRespository;
import com.infore.siteManagement.service.IFilterService;
import com.infore.siteManagement.util.ArrayUtils;
import com.infore.siteManagement.util.ResultEntity;

@Service
public class StatisAndAlarmServiceImpl implements IStatisAndAlarmService{

	@Autowired
	private IProjectAndStationRepository iProjectAndStationRepository;
	@Autowired
	private IStationRepository iStationRepository;
	@Autowired
	private IAreaRespository iAreaRespository;
	@Autowired
	private IBasinnRespository iBasinnRespository;
	@Autowired
	private IFilterService iFilterService;
	@Autowired
	private IndexRespository indexRespository;
	
	public ResultEntity queryProjectByStationMn(String stationMn) {
		List<String> mnList = ArrayUtils.stringToList(stationMn);
		//List<ProjectAndStation> projectAndStations = iProjectAndStationRepository.findByStationCodeIn(mnList);
		//List<String> list = projectAndStations.stream().map(l ->l.getProjectCode()).distinct().collect(Collectors.toList());
		List<Map<String, Object>> projectAndStations = iProjectAndStationRepository.QueryByStationCodeIn(mnList);
		List<String> list = projectAndStations.stream()
				.filter(f->f.get("PROJECT_CODE") != null)
				.map(m->m.get("PROJECT_CODE").toString())
				.collect(Collectors.toList());
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}

	public ResultEntity queryNameByStationMn(String stationMn) {
		List<String> mn = ArrayUtils.stringToList(stationMn);
		List<Map<String, Object>> maps = iStationRepository.queryStationNameByMn(mn);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(maps);
	}

	public ResultEntity queryFrequencyByStationMn(String stationMn, String projId) {	
		List<String> mn = ArrayUtils.stringToList(stationMn);
		List<Map<String, Object>> maps = iStationRepository.queryFrequencyByStationMn(mn, projId);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(maps);
	}
	
	@Override
	public ResultEntity queryStandardByStationMn(String stationMn, String projId) {
		List<String> mn = ArrayUtils.stringToList(stationMn);
		List<Map<String, Object>> maps = iStationRepository.queryStandardByStationMn(mn, projId);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(maps);
	}

	public ResultEntity queryStationInfoByCodes(String queryCode, String queryType,String userId,
			String platformId,String appCode,String admin) {
		List<Map<String, Object>> stations = new ArrayList<Map<String,Object>>();
		if ("area".equals(queryType)) {
			List<String> areaCode = ArrayUtils.stringToList(queryCode);
			stations = iStationRepository.queryStationInfoByFatherArea(areaCode);
		}else {
			List<String> basinnCode = ArrayUtils.stringToList(queryCode);
			stations = iStationRepository.queryStationInfoByFatherBasinn(basinnCode);
		}
		List<String> ids = iFilterService.getStationIdByCookie(platformId, appCode, userId, admin);
		stations = stations.stream().filter(f->ids.contains(f.get("STATION_ID").toString()))
				.collect(Collectors.toList());
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(stations);
	}

	public ResultEntity queryAreaByAreaCode(String queryCode, String queryType) {
		if ("area".equals(queryType)) {
			List<String> areaCode = ArrayUtils.stringToList(queryCode);
			List<Area> result = iAreaRespository.findAllByAreaCodeIn(areaCode);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(result);
		}else {
			List<String> basinnCode = ArrayUtils.stringToList(queryCode);
			List<Basinn> result = iBasinnRespository.findAllByBasinnCodeIn(basinnCode);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(result);
		}
	}

	public ResultEntity queryAllInfoByMn(String queryCode) {
		List<String> stationMnList = ArrayUtils.stringToList(queryCode);
		List<Station> list = iStationRepository.findAllByStationMnIn(stationMnList);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}

	public ResultEntity queryProjectByMn(List<String> mns) {
		List<Project> list = indexRespository.queryAllByMns(mns);
		Map<String, Project> resultMap = new HashMap<String, Project>();
		for (Project project : list) {
			resultMap.put(project.getProjectCode(), project);
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setInfo(resultMap);
	}

	public ResultEntity queryInfoByMnSdcjgyy(String stationMns) {
		List<String> mns = ArrayUtils.stringToList(stationMns);
		List<Map<String, Object>> list = iStationRepository.queryStationInfoByMns(mns);
		return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
	}



}
